﻿using FirstLimitUpMode.Handles;
using GpCommon.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static GpCommon.Helpers.MouseHook;

namespace FirstLimitUpMode.Views
{
    /// <summary>
    /// PointView.xaml 的交互逻辑
    /// </summary>
    public partial class PointView : Window
    {
        private MouseHook _mouseHook;
        private PointView()
        {
            InitializeComponent();

            _mouseHook = new MouseHook();
            _mouseHook.SetHook();
            _mouseHook.MouseMoveEvent -= MouseMoveExecute;
            _mouseHook.MouseMoveEvent += MouseMoveExecute;
        }

        public static PointView NewInstance(int index)
        {
            PointView view = new PointView();
            view.Topmost = true;
            view.NumBlock.Text = index.ToString();
            return view;
        }
        private void MouseMoveExecute(object sender, MyMouseEventArgs e)
        {
            if (isButtonDown)
            {
                if (GetCursorPos(out POINT pt))
                {
                    this.Left = pt.X - 15;
                    this.Top = pt.Y - 15;
                }
            }
        }

        private bool isButtonDown = false;
        private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isButtonDown = true;
        }

        private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isButtonDown = false;
        }

        private void Ellipse_MouseMove(object sender, MouseEventArgs e)
        {
            //if (isButtonDown)
            //{
            //    if (GetCursorPos(out POINT pt))
            //    {
            //        this.Left = pt.X - 15;
            //        this.Top = pt.Y - 15;
            //    }
            //}
        }

        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);

        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _mouseHook.UnHook();
        }
    }
}
